/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alxbrendmaquinarias.service;

import com.alxbrendmaquinarias.model.Product;
import com.alxbrendmaquinarias.model.ProductShop;
import com.alxbrendmaquinarias.model.Shop;
import com.alxbrendmaquinarias.model.Storehouse;
import com.alxbrendmaquinarias.model.StorehouseProduct;
import com.alxbrendmaquinarias.repository.GenericDAO;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Max
 */
@Service
public class ProductServiceImpl implements ProductService{
    @Autowired
    GenericDAO genericDAO;

    @Override
    public List list(String namedQuery) throws Exception {
        return genericDAO.findWithNamedQuery(namedQuery);
    }

    @Override
    public void save(Product product) throws Exception {
        Product pro = genericDAO.create(product);
        //AUDIT A LAS TABLAS CORRESPONIENTES
        List<Shop> list = genericDAO.findWithNamedQuery("Shop.findAll");
        for (Shop list1 : list) {
            ProductShop ps = new ProductShop();
            ps.setAmount(0);
            ps.setOutputPrice(0.0);
            ps.setProductId(pro);
            ps.setShopId(list1);
            ps.setTypeMoney("SOLES");
            genericDAO.create(ps);
        }
        List<Storehouse> listSH = genericDAO.findWithNamedQuery("Storehouse.findAll");
        for (Storehouse listSH1 : listSH) {
            StorehouseProduct sp = new StorehouseProduct();
            sp.setAmount(0);
            sp.setProductId(pro);
            sp.setStorehouseId(listSH1);
            genericDAO.create(sp);
        }
    }

    @Override
    public void update(Product product) throws Exception {
        genericDAO.update(product);
    }

    @Override
    public void remove(Product product) throws Exception {
        genericDAO.delete(product);
    }

    @Override
    public List<Product> maxProductId(){
        return genericDAO.findWithNativeQuery("SELECT MAX(product_id) FROM product", Product.class);
    }

    
}
